const invalidVal = '--';

const fixZero = (num: number, length: number) => {
    const str = '' + num;
    const len = str.length;
    let s = '';
    for (let i = length; i-- > len; ) {
        s += '0';
    }
    return s + str;
};
/**
 * 时间戳转时间
 * @param stamp 时间戳（数字或字符串）
 * @param type 可传数字1～13
 * @param defaultVal 默认为'--'
 * @returns 时间
 */
export const stampToTime = (dateNum, type: number, otherType = 'seconds', defaultVal = invalidVal) => {
    if (!dateNum) {
        if (dateNum !== 0) {
            return defaultVal;
        }
    }
    if (typeof dateNum == 'string') {
        if (dateNum.indexOf('-') == -1) {
            if (dateNum.length == 13) {
                dateNum = parseInt(dateNum);
            } else {
                return dateNum;
            }
        }
    }
    const date = new Date(dateNum);
    const month = date.getMonth() + 1;
    if (type == 1) {
        //2018-5-5 12:07:05
        return date.getFullYear() + '-' + month + '-' + date.getDate() + ' ' + fixZero(date.getHours(), 2) + ':' + fixZero(date.getMinutes(), 2) + ':' + fixZero(date.getSeconds(), 2);
    }
    if (type == 2) {
        //2018-05-05 12:07:05
        return date.getFullYear() + '-' + fixZero(month, 2) + '-' + fixZero(date.getDate(), 2) + ' ' + fixZero(date.getHours(), 2) + ':' + fixZero(date.getMinutes(), 2) + ':' + fixZero(date.getSeconds(), 2);
    }
    if (type == 3) {
        //2018/5/5 12:07:05
        return date.getFullYear() + '/' + month + '/' + date.getDate() + ' ' + fixZero(date.getHours(), 2) + ':' + fixZero(date.getMinutes(), 2) + ':' + fixZero(date.getSeconds(), 2);
    }
    if (type == 4) {
        //2018/05/05 12:07:05
        return date.getFullYear() + '/' + fixZero(month, 2) + '/' + fixZero(date.getDate(), 2) + ' ' + fixZero(date.getHours(), 2) + ':' + fixZero(date.getMinutes(), 2) + ':' + fixZero(date.getSeconds(), 2);
    }
    if (type == 5) {
        //2018-5-5
        return date.getFullYear() + '-' + month + '-' + date.getDate();
    }
    if (type == 6) {
        //2018-05-05
        return date.getFullYear() + '-' + fixZero(month, 2) + '-' + fixZero(date.getDate(), 2);
    }
    if (type == 7) {
        //2018/5/5
        return date.getFullYear() + '/' + month + '/' + date.getDate();
    }
    if (type == 8) {
        //2018/05/05
        return date.getFullYear() + '/' + fixZero(month, 2) + '/' + fixZero(date.getDate(), 2);
    }
    if (type == 9) {
        if (otherType == 'seconds') {
            //20180505120705
            return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2) + fixZero(date.getHours(), 2) + fixZero(date.getMinutes(), 2) + fixZero(date.getSeconds(), 2);
        } else if (otherType == 'minutes') {
            //201805051207
            return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2) + fixZero(date.getHours(), 2) + fixZero(date.getMinutes(), 2);
        } else if (otherType == 'hours') {
            return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2) + fixZero(date.getHours(), 2);
        } else if (otherType == 'day') {
            return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2);
        }
    }
    if (type == 10) {
        //2018年05月05日
        return date.getFullYear() + '年' + fixZero(month, 2) + '月' + fixZero(date.getDate(), 2) + '日';
    }
    if (type == 11) {
        //2018年05月05日12时07分05秒
        return date.getFullYear() + '年' + fixZero(month, 2) + '月' + fixZero(date.getDate(), 2) + '日' + fixZero(date.getHours(), 2) + '时' + fixZero(date.getMinutes(), 2) + '分' + fixZero(date.getSeconds(), 2) + '秒';
    }
    if (type == 12) {
        //180505
        return (date.getFullYear() + '').slice(-2) + fixZero(month, 2) + fixZero(date.getDate(), 2);
    }
    if (type == 13) {
        //2018-05-05 12:07
        return date.getFullYear() + '-' + fixZero(month, 2) + '-' + fixZero(date.getDate(), 2) + ' ' + fixZero(date.getHours(), 2) + ':' + fixZero(date.getMinutes(), 2);
    }
};

/**
 * 时间字符串转换为时间戳
 * @param timeStr 时间字符串
 * @returns 10位数的时间戳
 */
export const convertToStamp = (timeStr: string) => {
    const date = new Date(timeStr);
    return +date;
};

/**
 * 时间转时间戳
 * @param time
 * @param type 'begin':开始时间，'end':结束时间
 * @returns 时间戳数字
 */
export const timeToStamp = (time: Date, type?: string) => {
    const date = new Date(time);
    if (type && type == 'begin') {
        const year = date.getFullYear();
        const month = date.getMonth();
        const day = date.getDate();
        return +new Date(year, month, day);
    }
    if (type && type == 'end') {
        date.setHours(23);
        date.setMinutes(59);
        date.setSeconds(59);
        date.setMilliseconds(0);
        return +date;
    }
    return +date;
};

/**
 * 计算时间差值
 * @param beginTime 开始时间的时间戳
 * @param endTime 结束时间的时间戳
 * @param type 'day'/'hour'/'min'/'second'
 * @returns 时间差值
 */
export const timeCalculate = (beginTime: number, endTime: number, type: string) => {
    const newBegin = new Date(stampToTime(endTime, 5)!);
    const newEnd = new Date(stampToTime(beginTime, 5)!);
    const calculate = Number(newBegin) - Number(newEnd);
    let result = 0;
    if (type == 'day') {
        result = calculate / 1000 / 60 / 60 / 24;
        return Math.abs(Math.round(result));
    }
    if (type == 'hour') {
        result = calculate / 1000 / 60 / 60;
        return Math.abs(Math.round(result));
    }
    if (type == 'min') {
        result = calculate / 1000 / 60;
        return Math.abs(Math.round(result));
    }
    if (type == 'second') {
        result = calculate / 1000;
        return Math.abs(Math.round(result));
    }
};

export default {
    stampToTime,
    convertToStamp,
    timeToStamp,
    timeCalculate,
};
